package br.edu.fasete.javaroad.model.architeture;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VariableList implements Serializable {
	private static final long serialVersionUID = 1L;
	private Map<String, Variable> variaveis;
	
	public VariableList() {
		variaveis = new HashMap<String, Variable>();
	}
	
	public String getValue(String name) {
		Variable var = variaveis.get(name);
		if(var == null) {
			return "";
		}
		else 
			return var.getValue();
	}
	
	public void updateKeys() {
		List<String> wrongKeys = new ArrayList<String>();
		for(String k : variaveis.keySet()) {
			Variable v = variaveis.get(k);
			if(!k.equals(v.getName())) {
				wrongKeys.add(k);
				
			}
		}
		for(String k : wrongKeys) {
			Variable v = variaveis.get(k);
			variaveis.remove(k);
			variaveis.put(v.getName(), v);
		}
	}
	
	public void add(Variable var) throws DuplicatedVariableException{
		if(!variaveis.containsKey(var.getName())) {
			variaveis.put(var.getName(), var);
		} else {
			throw new DuplicatedVariableException(var.getName());
		}
	}
	
	public void setValue(String name, String val){
		Variable var = variaveis.get(name);
		if(var != null)
			var.setValue(val);
	}
	
	public void remove(String name) {
		variaveis.remove(name);
	}
	
	public Collection<Variable> getList() {
		return variaveis.values();
	}
}
